home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / hardware / mount_media / cdmount / cdmount.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.4 KB  |  157 lines

  1. /*
  2. **  Subsystem:   User Level mount for CD-ROM
  3. **  File Name:   cdmount.c
  4. **                                                        
  5. ** This software is Copyright (c) 1991 by Kent Landfield.
  6. **
  7. ** Permission is hereby granted to copy, distribute or otherwise 
  8. ** use any part of this package as long as you do not try to make 
  9. ** money from it or pretend that you wrote it.  This copyright 
  10. ** notice must be maintained in any copy made.
  11. **
  12. ** Use of this software constitutes acceptance for use in an AS IS 
  13. ** condition. There are NO warranties with regard to this software.  
  14. ** In no event shall the author be liable for any damages whatsoever 
  15. ** arising out of or in connection with the use or performance of this 
  16. ** software.  Any use of this software is at the user's own risk.
  17. **
  18. **  If you make modifications to this software that you feel 
  19. **  increases it usefulness for the rest of the community, please 
  20. **  email the changes, enhancements, bug fixes as well as any and 
  21. **  all ideas to me. This software is going to be maintained and 
  22. **  enhanced as deemed necessary by the community.
  23. **              
  24. **              Kent Landfield
  25. **              sparky!kent
  26. **              kent@sparky.imd.sterling.com
  27. */
  28. #if !defined(lint) && !defined(SABER)
  29. static char SID[] = "@(#)cdmount.c    1.2 8/19/91";
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35.  
  36. #define MNTPOINT "/cdrom"
  37.  
  38. void usage(progname)
  39. char *progname;
  40. {
  41.     (void) fprintf(stderr, "\nusage: %s [ -dhv ]\
  42. \n\noptions:\n\
  43.       -d   show the mount command without executing it\n\
  44.       -h   mount an ISO 9660 or High Sierra CD_ROM Filesystem\n\
  45.       -v   show the mount command and execute it\n\
  46. \n", progname);
  47. }
  48.  
  49. int main(argc, argv)
  50. int argc;
  51. char **argv;
  52. {
  53.     int getopt();
  54.     char *strrchr();
  55.  
  56.     extern char *optarg;
  57.     extern int optind;
  58.     extern int opterr;
  59.  
  60.     char *cp;
  61.     char cmd[256];
  62.     int rc;
  63.     int debug;
  64.     int iso9660;
  65.     struct stat stb;
  66.  
  67.     if ((cp = strrchr(argv[0],'/')) != NULL)
  68.         ++cp;
  69.     else 
  70.         cp = argv[0];
  71.  
  72.     /*
  73.     ** Setup IFS for system() protection...
  74.     */
  75.     if (putenv("IFS= \t\n") != 0) {
  76.          (void) fprintf(stderr,"%s: IFS putenv failed...\n", cp);
  77.          return(1);
  78.     }
  79.  
  80.     /*
  81.     ** Setup PATH for execlp() protection...
  82.     */
  83.     if (putenv("PATH=/etc:/usr/etc:/bin:/usr/bin") != 0) {
  84.          (void) fprintf(stderr,"%s: PATH putenv failed...\n", cp);
  85.          return(1);
  86.     }
  87.  
  88.     /*
  89.     ** If the user is requesting to mount a CD..
  90.     */
  91.     if (strcmp(cp, "cdmount") == 0) {
  92.         iso9660 = 0;
  93.         opterr = 0;
  94.         debug = 0;
  95.         /*
  96.         ** Assure that the mount point is there and in a 
  97.         ** directory and not a symbolic link ..
  98.         */
  99.         if (lstat(MNTPOINT, &stb) != 0) {
  100.             (void) fprintf(stderr, "%s: mount point missing\n", MNTPOINT);
  101.             return(1);
  102.         }
  103.         if ((stb.st_mode & S_IFMT) != S_IFDIR) {
  104.             (void) fprintf(stderr, "%s: invalid mount point\n", MNTPOINT);
  105.             return(1);
  106.         }
  107.  
  108.         if (argc > 1) {
  109.            while ((rc = getopt(argc, argv, "dhv")) != EOF) {
  110.               switch (rc) {
  111.                   case 'd':     /* debugging - does not run command. */
  112.                       debug = 1;
  113.                       break;
  114.                   case 'v':     /* verbose - runs command. */
  115.                       debug = 2;
  116.                       break;
  117.                   case 'h':
  118.                       /*
  119.                       ** mount an ISO 9660 Standard or High
  120.                       ** Sierra Standard CD-ROM filesystem.
  121.                       */
  122.                       iso9660++;
  123.                       break;
  124.                   default:
  125.                       usage(cp);
  126.                       return(1);
  127.               }
  128.            }   
  129.         }   
  130.  
  131.         /* build the command line.. */
  132.         
  133.         (void) sprintf(cmd, "/etc/mount -r %s -o nosuid /dev/sr0 %s",
  134.                        iso9660 ? "-t hsfs" : "", MNTPOINT);
  135.         if (debug) 
  136.             (void) fprintf(stderr, "%s\n", cmd);
  137.         if (debug != 1)
  138.             rc = system(cmd);
  139.     }
  140.  
  141.     /*
  142.     ** The user is requesting to dismount a CD...
  143.     */
  144.     else if (strcmp(cp, "cdumount") == 0) {
  145.         rc = system("/etc/umount /dev/sr0 && /usr/bin/eject /dev/sr0 2>/dev/null");
  146.     }
  147.  
  148.     /*
  149.     ** Improperly named/linked executables, I'm confused...
  150.     */
  151.     else {
  152.         (void) fprintf(stderr, "%s: I don't know who I am... ? \n", cp);
  153.         rc = 1;
  154.     }
  155.     return(rc >> 8);
  156. }
  157.